/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.util.logsystem;

import java.io.PrintWriter;


/**
 * Console.
 *
 * @author Sowrov
 * @version %I% %G%
 */
public class Console implements OutputDevice {
    PrintWriter output;
    boolean fresh;

    public Console() {
        this.fresh = true;
        this.output = null;
    } // end Console()

    public boolean isFreshDevice() {
        return this.fresh;
    } // end isFreshDevice()

    public void close() {
        if (this.output != null) {
            this.output.close();
        } // end if
    } // end close()

    public void flush() {
        if (this.output != null) {
            this.output.flush();
        } // end if
    } // end flush()

    public void init() {
        this.fresh = true;

        if (System.console() != null) {
            this.output = System.console().writer();
        } // end if
        else {
            this.output = null;
        } // end else
    } // end init()

    public synchronized void write(final String str) {
        this.fresh = false;

        if (this.output != null) {
            this.output.write(str);
            this.output.flush();
        } // end if
        else {
            System.out.println(str);
        } // end else
    } // end write()
} // end Console
